package com.bear.fastrepair.utils

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import androidx.core.app.ActivityCompat
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils

/**
 * @description
 * @author jacky
 * @date 2021/11/17-13:01
 */
object LocationHelper {
    /**
     * 获取定位信息
     * @param context Context
     * @param locationInfo Function1<AMapLocation?, Unit>
     */
    suspend inline fun getLocationInfo(
        context: Context,
        crossinline locationInfo: ((AMapLocation?) -> Unit)
    ) {
        val locationClient = AMapLocationClient(context)
        val locationOption = AMapLocationClientOption()

        locationClient.setLocationListener {
            if (it?.errorCode == 0) {
                locationInfo.invoke(it)
            } else {
                LogUtils.e("amap error code is ${it?.errorCode}")
                locationInfo.invoke(null)
            }
            locationClient.stopLocation()
            locationClient.onDestroy()
        }
        locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
//        locationOption.interval = 5000
        locationOption.isNeedAddress = true
        locationOption.isWifiScan = true
        locationOption.isMockEnable = false
        locationOption.httpTimeOut = 10000
        locationOption.isLocationCacheEnable = true
        locationOption.isOnceLocation = true
        locationClient.setLocationOption(locationOption)
        locationClient.startLocation()
    }

    /**
     * 获取定位信息
     * @param context Context
     * @param locationInfo Function1<AMapLocation?, Unit>
     */
    inline fun getLocationInfoNormal(
        context: Context,
        crossinline locationInfo: ((AMapLocation?) -> Unit)
    ) {
        val locationClient = AMapLocationClient(context)
        val locationOption = AMapLocationClientOption()

        locationClient.setLocationListener {
            if (it?.errorCode == 0) {
                locationInfo.invoke(it)
            } else {
                LogUtils.e("amap error code is ${it?.errorCode}")
                locationInfo.invoke(null)
            }
            locationClient.stopLocation()
            locationClient.onDestroy()
        }
        locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
//        locationOption.interval = 2 * 60 * 1000L
        locationOption.isNeedAddress = true
        locationOption.isWifiScan = true
        locationOption.isMockEnable = false
        locationOption.httpTimeOut = 10000
        locationOption.isLocationCacheEnable = true
        locationOption.isOnceLocation = true
        locationClient.setLocationOption(locationOption)
        locationClient.startLocation()
    }

    fun getLocationLocal(context: Context, block: (Double, Double) -> Unit) {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        val list = locationManager.getProviders(true)
        val locationProvider = when {
            list.contains(LocationManager.GPS_PROVIDER) -> {
                LocationManager.GPS_PROVIDER
            }
            list.contains(LocationManager.NETWORK_PROVIDER) -> {
                LocationManager.NETWORK_PROVIDER
            }
            else -> {
                ToastUtils.showShort("无法获取定位信息，请检查定位权限")
                return
            }
        }
        if (ActivityCompat.checkSelfPermission(
                context,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                context,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ToastUtils.showLong("无法获取定位信息，请检查定位权限")
            return
        }
        locationManager.requestLocationUpdates(locationProvider, 3000, 1F) {
            block(it.altitude, it.latitude)
        }
    }
}